package org.feng.fxfront.pages.animal;

import cn.hutool.core.bean.BeanUtil;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import lombok.Data;
import org.feng.api.animal.constant.AnimalSexEnum;
import org.feng.api.animal.constant.AnimalTypeEnum;
import org.feng.api.animal.request.AddAndUpdateAnimalRequest;
import org.feng.api.animal.response.QueryAnimalVo;
import org.feng.fxfront.pages.components.ComboBoxBaseEnumConvert;

import java.util.Arrays;

/**
 * 新增&更新动物弹出框
 *
 * @author feng
 */
@Data
public class AddAndUpdateAnimalDialog {

    private Dialog<ButtonType> dialog;

    private TextField nameTextField;
    private TextField ageTextField;
    private ComboBox<Integer> typeComboBox;
    private ComboBox<Integer> sexComboBox;
    private TextArea remarkTextArea;

    private QueryAnimalVo oldAnimal;

    public AddAndUpdateAnimalDialog(QueryAnimalVo animalVo) {
        Label nameLabel = new Label("名字 ");
        nameTextField = new TextField();

        Label ageLabel = new Label("年龄 ");
        ageTextField = new TextField();

        Label typeLabel = new Label("类型 ");
        typeComboBox = new ComboBox<>();
        typeComboBox.getItems().addAll(Arrays.stream(AnimalTypeEnum.values()).map(AnimalTypeEnum::getCode).toList());
        typeComboBox.setConverter(new ComboBoxBaseEnumConvert(AnimalTypeEnum.class));

        Label sexLabel = new Label("性别 ");
        sexComboBox = new ComboBox<>();
        sexComboBox.getItems().addAll(Arrays.stream(AnimalSexEnum.values()).map(AnimalSexEnum::getCode).toList());
        sexComboBox.setConverter(new ComboBoxBaseEnumConvert(AnimalSexEnum.class));

        Label remarkLabel = new Label("备注 ");
        remarkTextArea = new TextArea();
        remarkTextArea.setScrollTop(Double.MAX_VALUE);
        remarkTextArea.setEditable(true);
        remarkTextArea.setMaxHeight(250);
        remarkTextArea.setMaxWidth(300);

        // 更新&数据回显
        if (animalVo != null) {
            oldAnimal = new QueryAnimalVo();
            BeanUtil.copyProperties(animalVo, oldAnimal);

            // 数据回显
            nameTextField.setText(oldAnimal.getName());
            ageTextField.setText(String.valueOf(oldAnimal.getAge()));
            typeComboBox.setValue(oldAnimal.getAnimalTypeCode());
            sexComboBox.setValue(oldAnimal.getAnimalSexCode());
            remarkTextArea.setText(oldAnimal.getRemark());
        }

        // 定义网格面板
        GridPane gridPane = new GridPane();
        // 设置垂直间距，水平间距
        gridPane.setVgap(10);
        gridPane.setHgap(5);
        gridPane.add(nameLabel, 0, 0);
        gridPane.add(nameTextField, 1, 0);
        gridPane.add(ageLabel, 0, 1);
        gridPane.add(ageTextField, 1, 1);
        gridPane.add(typeLabel, 0, 2);
        gridPane.add(typeComboBox, 1, 2);
        gridPane.add(sexLabel, 0, 3);
        gridPane.add(sexComboBox, 1, 3);
        gridPane.add(remarkLabel, 0, 4);
        gridPane.add(remarkTextArea, 1, 4);

        DialogPane dialogPane = new DialogPane();
        dialogPane.setPrefHeight(300);
        dialogPane.setPrefWidth(500);
        dialogPane.setMaxHeight(400);
        dialogPane.setMaxWidth(550);

        dialogPane.setContent(gridPane);
        dialogPane.getButtonTypes().addAll(ButtonType.CANCEL, ButtonType.OK);
        dialog = new Dialog<>();
        dialog.setDialogPane(dialogPane);
    }

    public AddAndUpdateAnimalRequest genAddAndUpdateAnimalRequest() {
        AddAndUpdateAnimalRequest addAndUpdateAnimalRequest = new AddAndUpdateAnimalRequest();
        // 年龄
        addAndUpdateAnimalRequest.setAge(Integer.parseInt(this.getAgeTextField().getText()));
        // 姓名
        addAndUpdateAnimalRequest.setName(this.getNameTextField().getText());
        // 获取下拉框的数据：动物类型
        Integer type = this.getTypeComboBox().getValue();
        addAndUpdateAnimalRequest.setAnimalTypeCode(type);
        // 获取下拉框的数据：性别
        Integer sex = this.getSexComboBox().getValue();
        addAndUpdateAnimalRequest.setAnimalSexCode(sex);
        // 获取文本域内容：备注
        addAndUpdateAnimalRequest.setRemark(this.getRemarkTextArea().getText());
        return addAndUpdateAnimalRequest;
    }
}
